package edu.ubb.scrumster.controller.frontendController;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ubb.scrumster.controller.backendController.LoginController;
import edu.ubb.scrumster.model.model.User;



/**
 * Servlet implementation class LoginController
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginServlet() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
        String password = request.getParameter("password");
        LoginController ur=new LoginController();
        User u=ur.login(username, password);
        if (u==null){
        	request.setAttribute("errorMessage", "Invalid username or password!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        else
        if (u.getType().equals("member")){
        	request.getSession().setAttribute("user", u);     
        	response.sendRedirect("index.jsp");
        	
        }
        
	}

}
